package com.project.webapp.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;

public abstract class BaseAction extends ActionSupport implements RequestAware,
		SessionAware, ApplicationAware {

	/**
	 * <pre>
	 * 
	 * </pre>
	 */
	private static final long serialVersionUID = 1L;

	// 记录Action log
	public Logger log = Logger.getLogger(this.getClass());
	// struts自动注入request，session，application
	protected Map<String, Object> request;
	protected Map<String, Object> session;
	protected Map<String, Object> application;

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setApplication(Map<String, Object> application) {
		this.application = application;
	}

	/**
	 * 把对象输出到页面
	 * 
	 * @param obj
	 */
	public void ajaxPrintPage(Object obj) {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = null;
		try {
			try {
				writer = response.getWriter();
				if (null == obj)
					writer.print(obj);
				else
					writer.print(obj.toString());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} finally {
			if (writer != null) {
				writer.flush();
				writer.close();
			}
		}
	}
}
